/*
 * MIT License
 * 
 * Copyright (c) 2017 wen.gu <454727014@qq.com>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: tiny_parcel.h
 *
 * Purpose: parcel operation implementation
 *
 * Developer:
 *   wen.gu , 2016-12-09
 *
 * TODO: 
 *
 ***************************************************************************/

#ifndef __TINY_PARCEL_H__
#define __TINY_PARCEL_H__

#include "general_type.h"

#ifdef __cplusplus
extern "C" {
#endif
/***************************************************************************
 *
 * macro declaration
 *
 ***************************************************************************/

/***************************************************************************
 *
 * data structure declaration
 *
 ***************************************************************************/

/** @brief the data structure for parcel */
typedef struct tiny_parcel_s
{
    GU08* data; /**< pointer to data buffer */
    GU32 pos;   /**< offset position of parcel operation */
    GU32 size;  /**< the size of data buffer */  
}tiny_parcel_t;
/***************************************************************************
 *
 * API declaration
 *
 ***************************************************************************/

 /**
 *@brief  initialize parcel data structure
 *
 *@param  parcel [io] pointer to parcel data structure.
 *@param  data   [in] pointer to data buffer.
 *@param  len    [in] the length of out data()
 *
 *@retuen success: G_OK, fail: error code.
 *@see
 *
 *@note this interface set does not thread safe
 *
 */
 G_API GS32 tfParcelInitialize(tiny_parcel_t* parcel, void* data, GU32 len);

 /**
*@brief  write data to parcel
*
*@param  parcel [in] pointer to parcel data structure.
*@param  data   [in] pointer to data buffer.
*@param  len    [in] the length of data
*
*@retuen success: G_OK, fail: error code.
*@see
*/
G_API GS32 tfParcelWrite(struct tiny_parcel_s* parcel, const void* data, GU32 len);

/**
*@brief  write a 32 bits integer value to parcel
*
*@param  parcel [in] pointer to parcel data structure.
*@param  value  [in] the value of integer
*
*@retuen success: G_OK, fail: error code.
*@see
*/
G_API GS32  tfParcelWriteInt32(struct tiny_parcel_s* parcel, GS32 value);


/**
*@brief  write a 64 bits integer value to parcel
*
*@param  parcel [in] pointer to parcel data structure.
*@param  value  [in] the value of integer
*
*@retuen success: G_OK, fail: error code.
*@see
*/
G_API GS32 tfParcelWriteInt64(struct tiny_parcel_s* parcel, GS64 value);

/**
*@brief  write a float value to parcel
*
*@param  parcel [in] pointer to parcel data structure.
*@param  value  [in] the value of float
*
*@retuen success: G_OK, fail: error code.
*@see
*/
G_API GS32 tfParcelWriteFloat(struct tiny_parcel_s* parcel, GFLT value);

/**
*@brief  write a string to parcel
*
*@param  parcel [in] pointer to parcel data structure.
*@param  value  [in] pointer to string
*
*@retuen success: G_OK, fail: error code.
*@see
*
*@note the string must end with '\0'.
*
*/
G_API GS32 tfParcelWriteString(struct tiny_parcel_s* parcel, const char* value);

/**
*@brief  read data from parcel
*
*@param  parcel    [in]  pointer to parcel data structure.
*@param  out_data  [out] pointer to out data buffer.
*@param  len       [in]  the length of out data(request read)
*
*@retuen success: G_OK, fail: error code.
*@see
*/
G_API GS32 tfParcelRead(struct tiny_parcel_s* parcel, void* out_data, GU32 len);

/**
*@brief  read a 32 bits integer from parcel
*
*@param  parcel [in]  pointer to parcel data structure.
*
*@retuen the value of integer
*@see
*/
G_API GS32 tfParcelReadInt32(struct tiny_parcel_s* parcel);

/**
*@brief  read a 64 bits integer from parcel
*
*@param  parcel [in]  pointer to parcel data structure.
*
*@retuen the value of integer
*@see
*/
G_API GS64 tfParcelReadInt64(struct tiny_parcel_s* parcel);

/**
*@brief  read a float from parcel
*
*@param  parcel [in]  pointer to parcel data structure.
*
*@retuen the value of float
*@see
*/
G_API GFLT tfParcelReadFloat(struct tiny_parcel_s* parcel);

/**
*@brief  read a string from parcel
*
*@param  parcel [in]  pointer to parcel data structure.
*
*@retuen success: the pointer to string, fail: NULL
*@see
*/
G_API GSTR tfParcelReadString(struct tiny_parcel_s* parcel);

#ifdef __cplusplus
}
#endif

#endif //end of __TINY_PARCEL_H__
